package com.lma.crm.staff.domain.web.action;

import java.util.List;

import com.lma.crm.department.domain.CrmDepartment;
import com.lma.crm.department.service.CrmDepartmentService;
import com.lma.crm.staff.domain.CrmStaff;
import com.lma.crm.staff.service.CrmStaffService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CrmStaffAction extends ActionSupport implements ModelDriven<CrmStaff> {

	private static final long serialVersionUID = 1L;
	
	private CrmStaffService crmStaffService;
	public void setCrmStaffService(CrmStaffService crmStaffService) {
		this.crmStaffService = crmStaffService;
	}
	
	private CrmDepartmentService crmDepartmentService;
	public void setCrmDepartmentService(CrmDepartmentService crmDepartmentService) {
        this.crmDepartmentService = crmDepartmentService;
    }
	
	private CrmStaff crmStaff = new CrmStaff();
	@Override
	public CrmStaff getModel() {
		return crmStaff;
	}
	
	/////////////////////////////////////////////////////////////////////////
	
	/**
	 * 员工登陆方法
	 * @return
	 */
	public String login() {
		CrmStaff findCrmStaff = crmStaffService.login(crmStaff);
		if(null != findCrmStaff) {
			//登陆成功将员工信息放到session中
			ActionContext.getContext().getSession().put("loginStaff", findCrmStaff);
			return "success";
		}
		this.addFieldError("", "用户名或者密码错误");
		return "login";
	}

	public String home() {
		return "home";
	}
	
	public String findAll() {
	   
	   List<CrmStaff> crmStaffList = crmStaffService.findAll();
	   
	   /*
	    * 使用context存放数据
	    */
	   ActionContext.getContext().put("crmStaffList", crmStaffList);
	   
	   return "findAll"; 
	}
	
	/**
	 * 编辑员工信息前操作
	 * @return
	 */
	public String editUI() {
	    
	    //1.查询员工信息
	    CrmStaff findCrmStaff =crmStaffService.findById(crmStaff.getStaffId());
	    //2.将查询到的员工 信息放入到值栈中
	    ActionContext.getContext().getValueStack().push(findCrmStaff);
	    
	    //3.查询所有部门
	    List<CrmDepartment> findCrmDepartment = crmDepartmentService.findAll();
	    ActionContext.getContext().getValueStack().set("findCrmDepartment", findCrmDepartment);
	    
	    return "editUI";
	}
}
